package com.heima.search.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.search.dtos.HistorySearchDto;
import com.heima.model.search.pojos.ApUserSearch;
import com.heima.model.search.vo.SearchArticleVo;
import com.heima.model.user.pojos.ApUser;
import com.heima.search.mapper.ApUserSearchMapper;
import com.heima.search.service.ApUserSearchService;

import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import thread.AppThreadLocalUtil;

import java.util.Date;
import java.util.List;

@Service
public class ApUserSearchServiceImpl extends ServiceImpl<ApUserSearchMapper, ApUserSearch> implements ApUserSearchService {

    /**
     * 保存搜索记录到数据库
     *
     * @param keyword
     * @param userId
     */
    @Override
    public void insert(String keyword, Integer userId) {
        //1.查询当前用户的搜索关键词
        LambdaUpdateWrapper<ApUserSearch> lqw = new LambdaUpdateWrapper<>();
        lqw.eq(ApUserSearch::getKeyword, keyword).eq(ApUserSearch::getUserId, userId);
        ApUserSearch apUserSearch = getOne(lqw);
        //2.存在 更新创建时间
        if (apUserSearch != null) {
            apUserSearch.setCreatedTime(new Date());
            save(apUserSearch);
            return;
        }
        //3.不存在，判断当前历史记录总数量是否超过10
        ApUserSearch apUserSearch1 = new ApUserSearch();
        apUserSearch1.setKeyword(keyword);
        apUserSearch1.setCreatedTime(new Date());
        apUserSearch1.setUserId(userId);
        LambdaUpdateWrapper<ApUserSearch> lqw1 = new LambdaUpdateWrapper<>();
        lqw1.eq(ApUserSearch::getUserId, userId).orderByDesc(ApUserSearch::getCreatedTime);
        List<ApUserSearch> apUserSearches = list(lqw1);
        if (apUserSearches == null || apUserSearches.size() < 10) {
            save(apUserSearch1);
        } else {
            ApUserSearch apUserSearch2 = apUserSearches.get(apUserSearches.size() - 1);
            removeById(apUserSearch2);
            save(apUserSearch1);
        }
    }

    /**
     * 查询搜索历史
     *
     * @return
     */
    @Override
    public ResponseResult findUserSearch() {
        //获取当前用户
        ApUser user = AppThreadLocalUtil.getApUser();
        if (user == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }

        //根据用户查询数据，按照时间倒序
        List<ApUserSearch> apUserSearches = list(Wrappers.<ApUserSearch>lambdaQuery().eq(ApUserSearch::getUserId, user.getId()).orderByDesc(ApUserSearch::getCreatedTime));
        return ResponseResult.okResult(apUserSearches);
    }

    /**
     * 删除历史记录
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult delUserSearch(HistorySearchDto dto) {

        //1.检查参数
        if (dto.getId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //2.判断是否登录
        ApUser user = AppThreadLocalUtil.getApUser();
        if (user == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        remove(Wrappers.<ApUserSearch>lambdaQuery().eq(ApUserSearch::getUserId, user.getId()).eq(ApUserSearch::getId, dto.getId()));
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
